package com.example.administrator.newfragmentation.fragment.simple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.adpter.simple.MenuAdapter;
import com.example.administrator.newfragmentation.fragmentMain.BaseFragment;
import com.example.administrator.newfragmentation.listener.OnItemClickListener;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LeftFragment extends BaseFragment {
    private static final String ARG_PARAM = "left_list";
    @Bind(R.id.recy)
    RecyclerView recy;

    private ArrayList<String> mMenus;
    private int mCurrentPosition = -1;
    private MenuAdapter menuAdapter;

    private static final String SAVE_STATE_POSITION = "save_state_position";

    public static LeftFragment newInstance(ArrayList<String> menus) {
        Bundle args = new Bundle();
        args.putStringArrayList(ARG_PARAM, menus);

        LeftFragment fragment = new LeftFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mMenus = args.getStringArrayList(ARG_PARAM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_left, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager manager = new LinearLayoutManager(_mActivity);
        recy.setLayoutManager(manager);
        menuAdapter = new MenuAdapter(_mActivity);
        recy.setAdapter(menuAdapter);
        menuAdapter.setDatas(mMenus);

        menuAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                showContent(position);
            }
        });

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(SAVE_STATE_POSITION);
            menuAdapter.setItemChecked(mCurrentPosition);
        } else {
            mCurrentPosition = 0;
            menuAdapter.setItemChecked(0);
        }
    }

    private void showContent(int position) {
        if (position == mCurrentPosition) {
            return;
        }

        mCurrentPosition = position;

        menuAdapter.setItemChecked(position);

        ContentFragment fragment = ContentFragment.newInstance(mMenus.get(position));

        ((ShopFragment) getParentFragment()).switchContentFragment(fragment);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_STATE_POSITION, mCurrentPosition);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        recy.setAdapter(null);
        ButterKnife.unbind(this);
    }
}
